www.gusucode.com > VC++ ICPQ聊天室源程序-源码程序 > VC++ ICPQ聊天室源程序-源码程序/code/ChartRoomClient/ChartRoomClient.cpp

    // ChartRoomClient.cpp : Defines the class behaviors for the application.
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "ChartRoomClient.h"
#include "ChartRoomClientDlg.h"
#include "Splash.h"

//Global variable declare
SOCKET g_socket;
CString g_msg;
BOOL	g_bhasnamed = FALSE;
HANDLE  g_hlogin = NULL;
//ULNode* g_userlist = NULL;


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartRoomClientApp

BEGIN_MESSAGE_MAP(CChartRoomClientApp, CWinApp)
	//{{AFX_MSG_MAP(CChartRoomClientApp)
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartRoomClientApp construction

CChartRoomClientApp::CChartRoomClientApp()
{
	m_hmsgdlg = NULL;
	m_bfirstrecv = FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CChartRoomClientApp destruction

CChartRoomClientApp::~CChartRoomClientApp()
{
	CloseHandle(g_hlogin);
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CChartRoomClientApp object

CChartRoomClientApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CChartRoomClientApp initialization

BOOL CChartRoomClientApp::InitInstance()
{
	AfxEnableControlContainer();
	
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif
	
	g_hlogin = CreateEvent(NULL,TRUE,FALSE,NULL);
	CChartRoomClientDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
	}
	else if (nResponse == IDCANCEL)
	{
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

//Receive thread function
DWORD WINAPI CChartRoomClientApp::RecvThreadFunc(LPVOID lpParam)
{
	char		inbuf[1024];
	char		msg[30];
	int         rc;

	while(1)
	{
		if ((rc = recv(g_socket,inbuf,sizeof(inbuf),0)) == SOCKET_ERROR)
		{
			wsprintf(msg,"接收失败<错误号:%d>",GetLastError());
			::MessageBox(NULL,msg,"Error",MB_OK);
			closesocket(g_socket);
			WSACleanup();
			return -1;
		}
		if (!rc)			//perfectly close communication or be kick out
		{
	         if (closesocket(g_socket) == SOCKET_ERROR)
		     {
			    wsprintf(msg,"closesocket() failed with error %d", WSAGetLastError());
				::MessageBox(NULL,msg,"Error",MB_OK);
				return -1;
			 }
			 if(theApp.m_bfirstrecv) 
			 {
				 ::MessageBox(theApp.m_hmsgdlg,"你被踢出聊天室!","消息",MB_OK);
				 ::SendMessage(theApp.m_hmsgdlg,WM_CLOSE,NULL,NULL);
			 }
			 else 
			 {
				 g_bhasnamed = TRUE;
//				 ::SendDlgItemMessage(theApp.m_pMainWnd->GetSafeHwnd(),IDC_EXIT,BM_CLICK,0,0);
			 }
			 SetEvent(g_hlogin);
			 break;
		}
	  else
	  {
		g_msg += inbuf;
		g_msg += "\n\r";
		::SendMessage(theApp.m_hmsgdlg,WM_HASMSG,NULL,NULL);
	  }
	  if(!theApp.m_bfirstrecv) 
	  {
		  theApp.m_bfirstrecv = TRUE;			//have received first data ,
												//in order to know whether kick out or rename
		  SetEvent(g_hlogin);
	  }
	}
	return 0;
}

DWORD WINAPI CChartRoomClientApp::SendThreadFunc(LPVOID lpParam)
{
	char		outbuf[1024];
	char		msg[30];
	
	ZeroMemory(outbuf,sizeof(outbuf));

	strcpy(outbuf,(char *)lpParam);

	if (send(g_socket,outbuf,strlen(outbuf),0)
		== SOCKET_ERROR)
	{
		wsprintf(msg,"发送错误<错误号:%d>", WSAGetLastError());
		::MessageBox(NULL,msg,"Error",MB_OK);
		closesocket(g_socket);
		WSACleanup();
		return -1;
	}
	return 0;
}